
package com.innovamed.websocket.service;

import cn.hutool.core.net.NetUtil;
import com.innovamed.websocket.handler.SocketServer;
import com.innovamed.websocket.model.WsConfigInfo;
import com.innovamed.websocket.model.WsMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashSet;


@Service
public class WsService{
	
	@Autowired
	public Environment env;
	
	/**
	 * 发送/推送消息倒指定接收者;
	 * @param wsMsg
	 */
	public void sendMsgToSpecifiedRange(HttpServletRequest request, WsMsg wsMsg)
	{
		SocketServer.sendMsgToSpecifiedRange(wsMsg);
	}
	
	/**
	*    获取WS服务的配置信息;
	* @param request
	* @return
	*/
	@SuppressWarnings("deprecation")
	public WsConfigInfo getWsConfig(HttpServletRequest request)
	{
		WsConfigInfo wsConfigInfo = new WsConfigInfo();
		String serverPort = env.getProperty("server.port");
		LinkedHashSet<String> localIpv4s = NetUtil.localIpv4s();
		String localhostStr = NetUtil.getLocalhostStr();
		wsConfigInfo.setWsUri("/socketServer/{userId}");
		wsConfigInfo.setWsPort(serverPort);
		wsConfigInfo.setLocalIpSet(localIpv4s);
		wsConfigInfo.setLocalIpStr(localhostStr);
		return wsConfigInfo;
	}
	
}
